package com.apes.hr.base.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：编制调整计划
 * @author HuJianlin
 * @create 2018-04-25 15:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = StaffPlan.CONST_TABLE_NAME)
public class StaffPlan extends Model {
    public static final String CONST_TABLE_NAME = "hr_staff_plan";

    @Id
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "编制调整计划", prefix = "H")
    private String id; //计划单号

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司

    @Column(name = "DESCRIPTION", length = 500)
    private String description; //说明

    @Column(name = "state", length = 10, nullable = false)
    private String state; //状态： ('sent', '创建'), ('approve', '审核')

    @Column(name = "date_order")
    private Date dateOrder; //单据日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    private PartyRole createUid; //创建人

    @Column(name = "date_approve")
    private Date dateApprove; //审批日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    private PartyRole approveUid; //审批人

    @Column(name = "delete_date")
    private Date deleteDate; //删除日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    private PartyRole deleteUid; //删除人

    @OneToMany(mappedBy = "staffPlans", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<StaffPlanItem> staffPlanItems = new HashSet(); // 计划调整明细

    public Set<StaffPlanItem> getStaffPlanItems() {
        if (staffPlanItems == null) return null;
        for (StaffPlanItem items : staffPlanItems) {
            items.setStaffPlans(this);
        }
        return staffPlanItems;
    }


}
